package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.xcity.db.entity.Player;
import com.xcity.game.battle.pve.PVEManager;
import com.xcity.game.battle.pve.PVEStage;
import com.xcity.game.battle.pve.template.StageTemplate;
import com.xcity.game.buycount.BuyCount;
import com.xcity.game.item.drop.Drop;
import com.xcity.game.item.drop.impl.ExpDrop;
import com.xcity.game.item.drop.impl.GoldDrop;
import com.xcity.game.item.drop.impl.ItemDrop;
import com.xcity.game.vip.VipUtils;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class PVEStageV2Response extends Response {

	private int id;
	private int leftTimes; //剩余次数
	private int str; //消耗体力
	private int levelLimit; //角色等级限制
	private String name; //关卡名
//	private String desc; //关卡描述 客户端有存储，不用服务器发
	private DropInfoV2 rewards; //可掉落的奖励
	private int star; //星 0未通过 1~3=1~3星
	private List<Integer> sweepCounts;//可以连续扫荡次数的数组
	private int resetCount;//今日已重置的次数
	private int vipResetCount;//今日重置的总次数
	private int resetCost;//重置需要的钻石

	
	public static class StarInfo implements Comparable<StarInfo>{
		public int got;
		public String str;
		private int index;
		public StarInfo(String str, int got, int index){
			this.got = got;
			this.str = str;
			this.index = index;
		}
		@Override
		public int compareTo(StarInfo o) {
			if(this.got == o.got){
				return this.index - o.index;
			}
			return o.got - this.got;
		}
	}
	
	public static class DropInfoV2{
		private int gold;
		private int exp;
		private List<Drop> items = Collections.emptyList();
		public static final DropInfoV2 EMPTY = new DropInfoV2();
		public DropInfoV2(){}
		public DropInfoV2(List<Drop> drops, float addedRatio){
			items = new ArrayList<Drop>(drops.size());
			Set<Integer> itemIds = new HashSet<Integer>();
			for(int i = 0;i < drops.size(); i++){
				Drop d = drops.get(i);
				if(d instanceof GoldDrop){
					gold += ((GoldDrop) d).getGold() * (1 + addedRatio);
				}else if(d instanceof ExpDrop){
					exp += ((ExpDrop) d).getExp() * (1 + addedRatio);
				}else if(d instanceof ItemDrop){
					int itemid = ((ItemDrop)d).getId();
					if(!itemIds.contains(itemid)){
						itemIds.add(itemid);
						items.add(d);
					}
				}
			}
		}
		public int getGold() {
			return gold;
		}
		public void setGold(int gold) {
			this.gold = gold;
		}
		public int getExp() {
			return exp;
		}
		public void setExp(int exp) {
			this.exp = exp;
		}
		public List<Drop> getItems() {
			return items;
		}
		public void setItems(List<Drop> items) {
			this.items = items;
		}
	}
	public PVEStageV2Response(int serial, Player player, PVEStage stage) {
		super(serial);
		this.id = stage.getId();
		this.leftTimes = stage.getLeftTimes(player);

//		int vipSweepCount = VipUtils.getTemplate(player.getVipLvl()).getSweepCount();
//		if(vipSweepCount > this.leftTimes){
//			vipSweepCount = this.leftTimes;
//		}
		int leftCount = this.leftTimes;
		if(stage.getTemplate().getCostStr() > 0){
			int strCount = player.getVigour() / stage.getTemplate().getCostStr();
			if(leftCount > strCount){
				leftCount = strCount;
			}
		}
		
		this.sweepCounts = VipUtils.getSweepCountList(player.getVipLvl(), leftCount);
		
		if(stage.getTemplate().getType() == PVEManager.STAGE_TYPE_HERO){
			this.resetCount = stage.getTodayResetCount(player, (byte) 1);
			this.vipResetCount = VipUtils.getTemplate(player.getVipLvl()).getFullHeroStage();
			this.resetCost = BuyCount.PVE_HERO_RESET.getCostStone(player, this.resetCount);
		}else{
			this.resetCount = stage.getTodayResetCount(player, (byte) 0);
			this.vipResetCount = VipUtils.getTemplate(player.getVipLvl()).getFullNormalStage();
			this.resetCost = BuyCount.PVE_NORMAL_RESET.getCostStone(player, this.resetCount);
		}
//		this.resetCost = App.getApp().getServiceManager().get(BuyCountService.class).getCostStone(player, stage.getTemplate().getType() == PVEManager.STAGE_TYPE_HERO? BuyCountService.BUY_COUNT_TYPE_HERO : BuyCountService.BUY_COUNT_TYPE_NORMAL, this.resetCount, this.vipResetCount);
		
		
		StageTemplate template = stage.getTemplate();
		this.str = template.getCostStr();
		this.levelLimit = template.getLevelLimit();
		this.name = template.getName();
//		this.desc = template.getDesc();
		this.rewards = new DropInfoV2(template.getRewardsDrop(), 0f);
		this.star = stage.getStar();
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getLeftTimes() {
		return leftTimes;
	}
	public void setLeftTimes(int leftTimes) {
		this.leftTimes = leftTimes;
	}
	public int getStr() {
		return str;
	}
	public void setStr(int str) {
		this.str = str;
	}
	public int getLevelLimit() {
		return levelLimit;
	}
	public void setLevelLimit(int levelLimit) {
		this.levelLimit = levelLimit;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getStar() {
		return star;
	}
	public void setStar(int star) {
		this.star = star;
	}
	public DropInfoV2 getRewards() {
		return rewards;
	}
	public void setRewards(DropInfoV2 rewards) {
		this.rewards = rewards;
	}

	public List<Integer> getSweepCounts() {
		return sweepCounts;
	}

	public void setSweepCounts(List<Integer> sweepCounts) {
		this.sweepCounts = sweepCounts;
	}

	public int getResetCount() {
		return resetCount;
	}

	public void setResetCount(int resetCount) {
		this.resetCount = resetCount;
	}

	public int getVipResetCount() {
		return vipResetCount;
	}

	public void setVipResetCount(int vipResetCount) {
		this.vipResetCount = vipResetCount;
	}

//	public String getDesc() {
//		return desc;
//	}
//
//	public void setDesc(String desc) {
//		this.desc = desc;
//	}

	public int getResetCost() {
		return resetCost;
	}

	public void setResetCost(int resetCost) {
		this.resetCost = resetCost;
	}

}
